<?php

class Image {

    private $_height;
    private $_type;
    private $_uri;
    private $_uri150;
    private $_width;

    public function getHeight() {
        return $this->_height;
    }

    public function setHeight($height) {
        $this->_height = $height;
        return $this;
    }

    public function getType() {
        return $this->_type;
    }

    public function setType($type) {
        $this->_type = $type;
        return $this;
    }

    public function getUri() {
        return $this->_uri;
    }

    public function setUri($uri) {
        $this->_uri = $uri;
        return $this;
    }

    public function getUri150() {
        return $this->_uri150;
    }

    public function setUri150($uri150) {
        $this->_uri150 = $uri150;
        return $this;
    }

    public function getWidth() {
        return $this->_width;
    }

    public function setWidth($width) {
        $this->_width = $width;
        return $this;
    }

    function __construct(array $donnees) {
        foreach ($donnees as $key => $value) {
            if (isset($donnees[$key][0]['__content__'])) {
                $donnees[$key] = $donnees[$key][0]['__content__'];
            } else {
                $donnees[$key] = $donnees[$key][0];
            }
        }
        $this->hydrate($donnees);
    }

    public function __toArray() {
        return array('height' => $this->_height,
            'type' => $this->_type,
            'uri' => $this->_uri,
            'uri150' => $this->_uri150,
            'width' => $this->_width);
    }

    public function __toString() {
        return print_r($this->__toArray(), true);
    }

    public function hydrate(array $donnees) {
        foreach ($donnees as $key => $value) {
// On récupère le nom du setter correspondant à l'attribut.
            $method = 'set' . ucfirst($key);

// Si le setter correspondant existe.
            if (method_exists($this, $method)) {
// On appelle le setter.
                $this->$method($value);
            }
        }
    }

}

?>
